package com.itheima.test11;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static java.math.BigDecimal.ROUND_HALF_EVEN;

/*1.1 题目一
	有以下double数组：
		double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
	请编程计算它们的总值及平均值(四舍五入保留小数点后2位)
	ROUND_UP ， ROUND_DOWN ， ROUND_CEILING ， ROUND_FLOOR ， ROUND_HALF_UP ， ROUND_HALF_DOWN ， ROUND_HALF_EVEN ， ROUND_UNNECESSARY
*/
public class Test11 {
    public static void main(String[] args) {
        double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
        BigDecimal bigDecimal = new BigDecimal("1.0");
        int arrLenth = arr.length;


        BigDecimal sum = getSum(arr);

//        BigDecimal divide = sum.divide(bigDecimal,2,RoundingMode.HALF_UP);
        BigDecimal div= sum.divide(bigDecimal,4);
        System.out.println("总值为：" + div);

        String arrLenth1 = String.valueOf(arrLenth);
        BigDecimal arrLength = new BigDecimal(arrLenth1);
        System.out.println("平均分为：" + div.divide(arrLength,4));
    }

    private static BigDecimal getSum(double[] arr) {
        BigDecimal sum = new BigDecimal(0.0);
////        for (int i = 0; i < arr.length; i++) {
////            BigDecimal bigDecimal1 = new BigDecimal(arr[i]);
////            sum = sum.add(bigDecimal1);
////        }
////        return sum;



        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            String s =String.valueOf(arr[i]);
            BigDecimal b1 =new BigDecimal(s);
            sum=sum.add(b1);
//            stringBuilder.append(arr[i] + ",");
        }
//        String[] split = stringBuilder.toString().split(",");
//        for (int i = 0; i < split.length; i++) {
//            BigDecimal bigDecimal1 = new BigDecimal(split[i]);
//            sum = sum.add(bigDecimal1);
//        }
        return sum;
    }
}
